package tests;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import model.User;
import model.Token;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * This class tests the User tokens functionality
 * 
 * @author Sukhjinder K. Narula, Buthainah
 * @since June 2013
 *
 * @version $Revision: 1.0 $
 */
public class TestUserTokens {

	User user;
	HashMap<String, String> properties;
	List<Token> tokens;
	Token token1, token2;
	HashMap<String, String> tokensMap;
	
	/**
	 * This method is called before every test case, it creates a new user, sets its properties and adds two tokens to its
	 * list of tokens.
	 */
	@Before
	public void setUp()
	{
		user=new User("sknarula");
		properties=new HashMap<String,String>();
		properties.put("Id", "1");
		properties.put("Name","Laura");
		
		token1 = new Token(1, "place", "Montreal");
		token1.setId(1);
		token2 = new Token(1, "state", "QC");
		token2.setId(2);
		tokens = new ArrayList<Token>();
		tokens.add(token1);
		tokens.add(token2);
		
		user.setTokens(tokens);

		tokensMap = new HashMap<String,String>();
		for(Token token : tokens){
			tokensMap.put(token.getKey(), token.getValue());
		}
		
	}
	/**
	 * This method is called after every test case to tear down the user object and force garbage collection.
	 */
	
	@After
	public void tearDown()
	{
		user=null;
		System.gc();
	}
	
	/**
	 * This test case tests to see if the token exits return true if token exists
	 */
	@Test
	public void testTokenExistsReturnsTrue() {
		assertTrue(user.tokenExists(token1.getKey()));
	}

	/**
	 * This test case tests to see if the token exits return false if token does not exists
	 */
	@Test
	public void testTokenExistsReturnsFalse() {
		assertFalse(user.tokenExists("test"));
	}

	/**
	 * This test ensures that given an id, the get token method returns the correct object
	 */
	@Test
	public void testGetTokenById() {
		Token result = user.getToken(1);
		assertEquals(result, token1);
	}
	
	/**
	 * This test ensures that given an key, the get token method returns the correct object
	 */
	@Test
	public void testGetTokenByKey() {
		Token result = user.getToken("state");
		assertEquals(result, token2);
	}
	
	/**
	 * This test case tests the getTokens method by ensuring that the size of the list of tokens equals to the one specified.
	 */
	@Test
	public void testGetTokensBySize() {
		List<Token> result=user.getTokens();
		assertEquals(2,result.size());
	}

	/**
	 * This test case tests the getTokens method by ensuring that the list of the tokens return is equals to whats setup
	 */
	@Test
	public void testGetTokensByData() {
		List<Token> result=user.getTokens();
		assertEquals(result,tokens);
	}

	/**
	 * This test case tests the getTokensHashMap generate the same hashmap of tokens created as a part of setup
	 */
	@Test
	public void getTokenHashMap() {
		HashMap<String, String> result=user.getTokenHashMap();
		assertEquals(result,tokensMap);
	}

	/**
	 * This test ensures that transfer object for token is created appropriately
	 */
	@Test
	public void testGetTransferObjectToGetTemplateHashMap() {
		@SuppressWarnings("serial")
		HashMap<String, String> tokenTransferObject = new HashMap<String, String>(){{
			put("Id", String.valueOf(1));
			put("UserId", String.valueOf(1));
			put("Key", "place");
			put("Value",  "Montreal");
		}};
		assertEquals(tokenTransferObject, token1.getTransferObject());		
	}
	
	/**
	 * This test ensures that transfer object is created appropriately
	 */
	@Test
	public void testGetTransferObjectToGetEmptyHashMap() {
		HashMap<String, String> meetingTransferObject = new HashMap<String, String>();
		assertFalse(meetingTransferObject.equals(token1.getTransferObject()));
	}
	
	/**
	 * This test case tests the getUserId 
	 */
	@Test
	public void TestGetUserId()
	{
		assertEquals(1,token1.getUserId());
		
	}
	
	/**
	 * This test case tests the setUserId 
	 */
	@Test
	public void TestSetUserId()
	{
		token1.setUserId(2);
		assertEquals(2,token1.getUserId());
	}
	
	/**
	 * This test case tests the setValue method
	 *
	 */
	@Test
	public void TestSetValue() {
		token1.setValue("new value");
		assertEquals("new value",token1.getValue());
	}
	
	/**
	 * This test case tests the setKey method
	
	 */
	@Test
	public void TestSetkey() {
		token1.setkey("New key");
		assertEquals("New key",token1.getKey());
	}
	/**
	 * This test case tests the get transfer object by checking it's size and for null
	 */
	@Test
	public void TestGetTransferObject()
	{
		HashMap<String, String> result = new HashMap<String, String>();
		result=token1.getTransferObject();
		assertEquals(4,result.size());
		assertNotNull(result);
		
		
	}
}
